# 2024/12/28 
# file name: apriori.py


import pandas as pd

# 自定义连接函数，用于实现L_{k-1}到C_k的连接
def connect_string(x, ms):
    x = list(map(lambda i: sorted(str(i).split(ms)), x))
    l = len(x[0])
    r = []
    for i in range(len(x)):
        for j in range(i, len(x)):
            if x[i][:l - 1] == x[j][:l - 1] and x[i][l - 1] != x[j][l - 1]:
                r.append(x[i][:l - 1] + sorted([x[j][l - 1], x[i][l - 1]]))
    return r


# 寻找关联规则的函数
def find_rule(d, support, confidence, ms='--'):
    result = pd.DataFrame(index=['support', 'confidence'])  # 定义输出结果

    support_series = 1.0 * d.sum() / len(d)  # 支持度序列
    column = list(support_series[support_series > support].index)  # 初步根据支持度筛选
    k = 0

    while len(column) > 1:
        k = k + 1
        print(u'\n正在进行第%s次搜索...' % k)
        column = connect_string(column, ms)
        print(u'数目：%s...' % len(column))

        sf = lambda i: d[i].prod(axis=1)  # 新一批支持度的计算函数
        # 创建连接数据，这一步耗时、耗内存最严重。当数据集较大时，可以考虑并行运算优化。
        # print(sf)
        print(column)
        # print(list(map(sf, column)))
        d_2 = pd.DataFrame(list(map(sf, column)), index=[ms.join(i) for i in column]).T

        support_series_2 = 1.0 * d_2[[ms.join(i) for i in column]].sum() / len(d)  # 计算连接后的支持度
        column = list(support_series_2[support_series_2 > support].index)  # 新一轮支持度筛选
        # support_series = support_series.append(support_series_2)
        support_series = pd.concat([support_series, support_series_2], ignore_index=False)
        column2 = []

        for i in column:  # 遍历可能的推理，如{A,B,C}究竟是A+B-->C还是B+C-->A还是C+A-->B？
            i = i.split(ms)
            for j in range(len(i)):
                column2.append(i[:j] + i[j + 1:] + i[j:j + 1])

        cofidence_series = pd.Series(index=[ms.join(i) for i in column2])  # 定义置信度序列

        for i in column2:  # 计算置信度序列
            cofidence_series[ms.join(i)] = support_series[ms.join(sorted(i))] / support_series[ms.join(i[:len(i) - 1])]

        for i in cofidence_series[cofidence_series > confidence].index:  # 置信度筛选
            result[i] = 0.0
            # result[i]['confidence'] = cofidence_series[i]
            # result[i]['support'] = support_series[ms.join(sorted(i.split(ms)))]
            result.loc['confidence', i] = cofidence_series[i]
            result.loc['support', i] = support_series[ms.join(sorted(i.split(ms)))]

    result = result.T.sort_values(['confidence', 'support'], ascending=False)  # 结果整理，输出
    print(u'\n结果为：')
    print(result)
    print(type(result))
    return result

def loadDataSet():
    return [
        ['a', 'c', 'e'], ['b', 'd'], ['b', 'c'], ['a', 'b', 'c', 'd'],
        ['a', 'b'], ['b', 'c'], ['a', 'b'], ['a', 'b', 'c', 'e'], ['a', 'b', 'c'], ['a', 'c', 'e']
    ]

def transformDataSet(data):
    """
    转换为0-1矩阵
        a   b   c   d   e
        1   0   0   0   1
        0   1   0   1   0
        ...
    """
    # data_for_df = [list(i) for i in data]
    unique_elements = sorted(set(element for sublist in data for element in sublist))
    ct = lambda x: pd.Series({elem: 1 for elem in x if elem in unique_elements}, dtype=int)
    b = map(ct, data)
    new_data = pd.DataFrame(list(b)).fillna(0)
    return new_data


if __name__=='__main__':
    datasets = loadDataSet()
    result = transformDataSet(datasets)
    support = 0.2
    confidence = 0.5
    find_rule(result, support, confidence)
    


